﻿using System.Collections;
using IDAL;
using Model;

namespace BLL
{
    /// <summary>
    /// 普通User业务逻辑层
    /// </summary>
    public class UserService
    {
        private static readonly IUserDAL dal = DALFactory.DataAccess.createUserDAL();

        //登陆
        public Hashtable login(string username, string password)
        {
            Hashtable ht = new Hashtable();
            User u = dal.getUserByEmail(username);

            if (u == null)
            {
                ht.Add("result", "0");
                ht.Add("message", "账号不存在");
                return ht;
            }
            if (u.password != password)
            {
                ht.Add("result", "0");
                ht.Add("message", "密码不正确");
                return ht;
            }
            ht.Add("result", "1");
            ht.Add("message", "登陆成功");
            return ht;
        }

        //修改密码
        public Hashtable modifyPassword(User u, string password)
        {
            u.password = password;
            dal.updateUser(u);
            Hashtable ht = new Hashtable();
            ht.Add("result", "1");
            return ht;
        }

        //根据用户名得到用户
        public User getUserByEmail(string username)
        {
            User u = dal.getUserByEmail(username);
            return u;
        }

        //根据id得到用户
        public User getUserById(int id)
        {
            User u = dal.getUserById(id);
            return u;
        }

        //添加资源列表
        public void addResource(User user, Resource resource)
        {
            user.resourceList.Add(resource);
            dal.updateUser(user);
        }

    }
}
